package com.game.springbootstart.controller.user;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.OSSObject;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.properties.AliOssProperties;
import com.game.springbootstart.service.IGamesDetailsService;
import com.game.springbootstart.service.IGamesUrlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.web.ServerProperties;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2025-02-21
 */
@RestController
@RequestMapping("/games-url")
public class GamesUrlController {
    @Autowired
    private AliOssProperties aliOssProperties;
    @Autowired
    private IGamesUrlService gamesUrlService;
    @Autowired
    private IGamesDetailsService gamesDetailsService;

    @GetMapping("start")
    public ResponseEntity<String> startGames(@RequestParam Long id) {
        // 假设 gamesUrlService.startGames(id) 返回游戏资源的 URL
        String gameUrl = gamesUrlService.startGames(id);
        String fileKey = gameUrl.substring(gameUrl.lastIndexOf("/") + 1);
        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());

        try {
            // 从 OSS 获取文件
            OSSObject ossObject = ossClient.getObject(aliOssProperties.getBucketName(), fileKey);
            String fileContent = new String(ossObject.getObjectContent().readAllBytes(), StandardCharsets.UTF_8);

            // 包装为 HTML 格式
            String htmlContent = "<!DOCTYPE html><html><head><meta charset=\"UTF-8\"><title>Game</title></head><body>" + fileContent + "</body></html>";

            // 返回 HTML 内容
            return ResponseEntity.ok()
                    .contentType(new MediaType("text", "html", StandardCharsets.UTF_8))
                    .body(htmlContent);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to load game: " + e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        // 返回游戏资源的 URL，并设置正确的 Content-Type
    }

    @GetMapping("show")
    public ResponseEntity<String> showGames(@RequestParam Long id) {

        String gameUrl = gamesDetailsService.getGameUrl(id);
        String fileKey = gameUrl.substring(gameUrl.lastIndexOf("/") + 1);
        // 创建 OSS 客户端
        OSS ossClient = new OSSClientBuilder().build(aliOssProperties.getEndpoint(), aliOssProperties.getAccessKeyId(), aliOssProperties.getAccessKeySecret());

        try {
            // 从 OSS 获取文件
            OSSObject ossObject = ossClient.getObject(aliOssProperties.getBucketName(), fileKey);
            String fileContent = new String(ossObject.getObjectContent().readAllBytes(),StandardCharsets.UTF_8);

            // 包装为 HTML 格式
            String htmlContent = "<!DOCTYPE html><html><head><title>Game</title></head><body>" + fileContent + "</body></html>";

            // 返回 HTML 内容
            return ResponseEntity.ok()
                    .contentType(MediaType.TEXT_HTML)
                    .body(htmlContent);
        } catch (Exception e) {
            return ResponseEntity.internalServerError().body("Failed to load game: " + e.getMessage());
        } finally {
            ossClient.shutdown();
        }
        // 返回游戏资源的 URL，并设置正确的 Content-Type
    }
}
